from spark_utils import SparkSessionManager
"""
    读取mysql数据 并且写入gaussdb数据库的脚本
"""
spark = SparkSessionManager.get_spark_session()

mysql_url = "jdbc:mysql://${host}:${port}/py_spark"
msyql_properties = {
    "user": "${user}",
    "password": "${pwd}",
    "driver": "com.mysql.cj.jdbc.Driver"
}
table_name = "stu"
spark.read.jdbc(url=mysql_url, table=table_name, properties=msyql_properties).write \
    .format("jdbc") \
    .option("url", "jdbc:postgresql://${gauss_host}:${gauss_port}/db_all_userbase?currentSchema=db_pyspark") \
    .option("driver", "org.postgresql.Driver") \
    .option("dbtable", "stu") \
    .option("user", "${gauss_user}") \
    .option("password", "${gauss_pwd}") \
    .mode("overwrite") \
    .save()

# 关闭SparkSession
spark.stop()
